package com.apes.framework.plugin.fsm;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.outbox.annotation.OutBoxExclude;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：状态实例
 *
 * @author xul
 * @create 2020-08-27 11:37
 */
@OutBoxExclude
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FsmStateInstance.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "fsm_flow_instance_id, state_id")})
public class FsmStateInstance extends Model {
    public static final String CONST_TABLE_NAME = "fsm_state_inst";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "状态流：状态实例")
    @Comment("编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "state_id", referencedColumnName = "id", nullable = false)
    @Comment("状态")
    private FsmState state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fsm_flow_instance_id", referencedColumnName = "id", nullable = false)
    @Comment("状态流实例")
    private FsmFlowInstance fsmFlowInstance;

    @Column(name = "finished", columnDefinition = "number(1,0) default 0", nullable = false)
    @Comment("是否结束")
    private boolean finished = false;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @Comment("上级节点")
    private FsmStateInstance parent;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "child_id", referencedColumnName = "id")
    @Comment("子节点")
    private FsmFlowInstance child;

    @Column(name = "expired", updatable = false)
    @Comment("过期时间")
    private Date expired;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "event_id", referencedColumnName = "id")
    @Comment("事件")
    private FsmEvent event;

    @Column(name = "init_dept_id", length = 32)
    @Comment("发起部门")
    private String initDeptId;

    @Column(name = "executor", length = 32)
    @Comment("执行人")
    private String executor;

}
